/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.common.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.javaeye.common.dto.Code;

public class CodeDAOHibernate extends HibernateDaoSupport implements CodeDAO {

	@Override
	public Code getCode(int id) {
		return (Code)getHibernateTemplate().get(Code.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Code> getCodesByCategoryId(String categoryId) {
		final String queryString = "from Code c where c.categoryId=? order by c.id";
		return getHibernateTemplate().find(queryString, categoryId);
	}

	@Override
	public void removeCode(int id) {
		Code c = (Code)getHibernateTemplate().load(Code.class, id);
		getHibernateTemplate().delete(c);
	}

	@Override
	public void saveCode(Code code) {
		getHibernateTemplate().saveOrUpdate(code);
		logger.debug("Code id set to: " + code.getId());
	}

}
